import {createRouter,createWebHashHistory} from "vue-router"
import {staticRoute} from "./modules/staticRouter"
import {mainStore} from "@/stores"

const router = createRouter({
    history:createWebHashHistory(),
    routes:[...staticRoute],
    strict:false,
    scrollBehavior:()=>({left:0,top:0})
})

router.beforeEach((to,from,next)=>{
    let mStore = mainStore()

    // 3.判断是访问登陆页，有 Token 就在当前页面，没有 Token 重置路由并放行到登陆页
    if(to.path === "/login"){
        if(mStore.token) return next(from.fullPath)
        return next()
    }

    // 5.判断是否有 Token，没有重定向到 login
    if(!mStore.token) return next({
        path:"/login",
        replace:true
    })
    
    // 7.正常访问页面
    next()
})


export default router